<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Session;
use think\facade\Db;
use app\index\model\XiaoshuoList;
use app\index\model\XiaoshuoArchives;
use think\facade\Queue;
use think\cache\driver\Redis;
 

class Index extends BaseController
{

    public function index()
    {
        $uid = rand(1, 10000);
        $num = rand(1, 4);
        $orderData = [
            'uid' => $uid,
            'gid' => 2,
            'num' => $num,
            'orderid' => GetRandStr()
        ];
        $redis=new Redis();
        $jobQueueName = 'check';
        $i=0;
       // $redis->zadd('user',$i++,$uid);
   
        // $isPushed = Queue::push(\app\http\Jobs::class,$orderData,$jobQueueName);
        //$isPushed = Queue::later(3, \app\http\Jobs::class, $orderData, $jobQueueName);
      // $s= queue(\app\http\Jobs::class, $orderData,3,$jobQueueName);
       
    }

    public function show()
    {
        $id = \mt_rand(1, 10000);
        $pid = '1234';
        $redis = new Redis();
        $ukey = 'user';
        $num = $redis->get('num');
        
        if ($redis->sismember($ukey, $id)) {
            return \reMsg(- 1, '', '已参与');
        }
        
        if (empty($num) || $num == null || $num <= 0) {
            return '库存没有了';
        }
        // $redis->watch('num');
        // $redis->multi();
        $redis->decr('num');
        $redis->sadd($ukey, $id);
        // $redis->exec();
        return \reMsg(1, $ukey, '秒杀成功');
    }

    // 写入章节
    public function writeArchives($url)
    {
        $file = $this->httpRequest($url);
        $per = '/<div class=\"content\".*><div class=\"con_r\">.*<\/div><p class=\"contnew\">.*<\/p>([\s\S]*)<div class=\"con_l\">/iU';
        preg_match_all($per, $file, $arr);
        $content = preg_replace('/wwW.mhtwx.la|www.mhtwx.la|WWW.mhtwx.la|mhtwx.la/', '好看的小说', $arr[1][0]);
        $content = preg_replace('/无弹窗广告|棉花糖小说网/', '好看的小说', $content);
        return $content;
    }

    // 显示书
    public function books()
    {
        $id = \input('param.id');
        $result = XiaoshuoList::find($id)->toArray();
        $url = $result['url'];
        $file = $this->httpRequest($url);
        $per = '/<div class=\"novel_list\" .*>([\s\S]*)<\/div>/iU';
        preg_match_all($per, $file, $arr);
        
        $per = '/<dd><a href=\"(.*)\">([\s\S]*)<\/a><\/dd>/iU';
        preg_match_all($per, $arr[1][0], $arr);
        $data = [];
        foreach ($arr[1] as $key => $v) {
            $data[$key]['chaptername'] = $arr[2][$key];
            $data[$key]['arcurl'] = $url . $v;
            $data[$key]['fid'] = $id;
            $data[$key]['updatetime'] = time();
        }
        
        View::assign([
            'arr' => $result,
            'list' => $data
        ]);
        return View::fetch();
    }

    // 远程获取
    public function httpRequest($sUrl, $gzip = 'false', $aHeader = '', $aData = '')
    {
        $ch = curl_init();
        if (substr($sUrl, 0, 5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        }
        if (empty($aHeader)) {
            $aHeader = [
                "Content-type:application/json;charset=utf-8",
                "Accept:application/json"
            ];
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_POST, 0);
        if (! empty($aData)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($aData)); // 格式
        }
        if ($gzip)
            curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        $sResult = curl_exec($ch);
        if ($sError = curl_error($ch)) {
            die($sError);
        }
        curl_close($ch);
        return $sResult;
    }
}
